﻿using System;
using System.Collections.Generic;

namespace InstanceController.WADPerformanceCountersDAL
{
	/// <summary>
	/// This class handles the communications between the View and the data store. 
	/// Generally this is the business layer logic.  The intent is for the View to issues 
	/// commands such as insert, update, or delete types. And to issue data queries to
	/// read from data storage.  
	/// It is expected that the developer will use the Class Diagram tool to create 
	/// any additional datamodels as necessary.
	/// 
	/// The code below is sample. Your code will/should describe your business intents.
	/// </summary>
	public static class WADPerformanceCountersDataMethods
	{
		static readonly WADPerformanceCountersDataSource WADPerformanceCountersDataSource = new WADPerformanceCountersDataSource(true);

		public static void SaveWADPerformanceCounters(WADPerformanceCountersDataModel theDataModel)
		{
			//Todo: Business Decision: 
			//Determine your PartitionKey plan and add the partition key now. Also you must add 
			//a unique rowKey.
			//You could simply hardcode it or use a web.config variable, you might even call out
			//to some more sophisticated partition key manager of your own design.  This is a 
			//business driven issue.
			//theDataModel.PartitionKey = theDataModel.State;			//Example: Grouping by State (a property in the datamodel)
			//theDataModel.RowKey = Guid.NewGuid().ToString("N");		//Example: Generating a Guid as the rowKey

			//Todo: Might want to do additional business logic here

			//WADPerformanceCountersDataSource.Create(theDataModel);
		}

		public static void UpdateWADPerformanceCounters(WADPerformanceCountersDataModel theDataModel, bool overWrite)
		{
			//Todo: Might want to do additional business logic here

			if (overWrite)
				WADPerformanceCountersDataSource.UpdateUnconditional(theDataModel);
			else
				WADPerformanceCountersDataSource.Update(theDataModel);
		}

		public static bool DeleteWADPerformanceCounters(WADPerformanceCountersDataModel theDataModel, bool unConditional, bool asPartitionGroup)
		{
			//Todo: Might want to do additional business logic here

			if (true)	//add your evaluation here to determine when to delete (if ever)
			{
				if (asPartitionGroup)
				{
					if (WADPerformanceCountersDataSource.DeletePartitionGroupUnconditional(theDataModel) > 0)
						return true;
				}
				else
					return unConditional
					       	? WADPerformanceCountersDataSource.DeleteUnconditional(theDataModel)
					       	: WADPerformanceCountersDataSource.Delete(theDataModel);
			}
			return true;
		}

		/// <summary>
		/// Retrieves the performance counter values.
		/// Gets a list of counter data.  Based on a timestamp that is the nearest time and going back 
		/// the span of minutes specified a list of performance counter data is retured for a specified counter
		/// in a specified role and deployment.
		/// </summary>
		/// <param name="deploymentId">The deployment id.</param>
		/// <param name="roleName">Name of the role.</param>
		/// <param name="timestamp">The timestamp.</param>
		/// <param name="counterName">Name of the counter.</param>
		/// <param name="spanMinutes">The span minutes.</param>
		/// <returns></returns>
		public static List<WADPerformanceCountersDataModel> RetrievePerformanceCounterValues(string deploymentId, string roleName, DateTime timestamp, string counterName, double spanMinutes)
		{
			return WADPerformanceCountersDataSource.Read(deploymentId, roleName, timestamp, counterName, spanMinutes);

		}

		public static int ClearAllTableRows()
		{
			return WADPerformanceCountersDataSource.ClearTable();
		}
	}
}
